// Side Navigation Menu Slide

$(document).ready(function() {
	$("#nav > li > a.collapsed + ul").slideToggle("medium");
	$("#nav > li > a").click(function() {
		$(this).toggleClass('expanded').toggleClass('collapsed').parent().find('> ul').slideToggle('medium');
	});
});


// Notifications Pop-Up Code

/************************************************************************
 * @name: bPopup
 * @author: Bjoern Klinggaard (http://dinbror.dk/bpopup)
 * @version: 0.4.1.min
 ************************************************************************/ 
/*(function(a){a.fn.bPopup=function(f,j){function s(){var b=a("input[type=text]",c).length!=0,k=o.vStart!=null?o.vStart:d.scrollTop()+g;c.css({left:d.scrollLeft()+h,position:"absolute",top:k,"z-index":o.zIndex}).appendTo(o.appendTo).hide(function(){b&&c.each(function(){c.find("input[type=text]").val("")});if(o.loadUrl!=null){o.contentContainer=o.contentContainer==null?c:a(o.contentContainer);switch(o.content){case "ajax":o.contentContainer.load(o.loadUrl);break;case "iframe":a('<iframe width="100%" height="100%"></iframe>').attr("src",
o.loadUrl).appendTo(o.contentContainer);break;case "xlink":a("a#bContinue").attr({href:o.loadUrl});a("a#bContinue .btnLink").text(a("a.xlink").attr("title"))}}}).fadeIn(o.fadeSpeed,function(){b&&c.find("input[type=text]:first").focus();a.isFunction(j)&&j()});t()}function i(){o.modal&&a("#bModal").fadeOut(o.fadeSpeed,function(){a("#bModal").remove()});c.fadeOut(o.fadeSpeed,function(){o.loadUrl!=null&&o.content!="xlink"&&o.contentContainer.empty()});o.scrollBar||a("html").css("overflow","auto");a("."+
o.closeClass).die("click");a("#bModal").die("click");d.unbind("keydown.bPopup");e.unbind(".bPopup");c.data("bPopup",null);return false}function u(){if(m){var b=[d.height(),d.width()];return{"background-color":o.modalColor,height:b[0],left:l(),opacity:0,position:"absolute",top:0,width:b[1],"z-index":o.zIndex-1}}else return{"background-color":o.modalColor,height:"100%",left:0,opacity:0,position:"fixed",top:0,width:"100%","z-index":o.zIndex-1}}function t(){a("."+o.closeClass).live("click",i);o.modalClose&&
a("#bModal").live("click",i).css("cursor","pointer");o.follow&&e.bind("scroll.bPopup",function(){c.stop().animate({left:d.scrollLeft()+h,top:d.scrollTop()+g},o.followSpeed)}).bind("resize.bPopup",function(){if(o.modal&&m){var b=[d.height(),d.width()];n.css({height:b[0],width:b[1],left:l()})}b=p(c,o.amsl);g=b[0];h=b[1];c.stop().animate({left:d.scrollLeft()+h,top:d.scrollTop()+g},o.followSpeed)});o.escClose&&d.bind("keydown.bPopup",function(b){b.which==27&&i()})}function l(){return e.width()<a("body").width()?
0:(a("body").width()-e.width())/2}function p(b,k){var q=(e.height()-b.outerHeight(true))/2-k,v=(e.width()-b.outerWidth(true))/2+l();return[q<20?20:q,v]}if(a.isFunction(f)){j=f;f=null}o=a.extend({},a.fn.bPopup.defaults,f);o.scrollBar||a("html").css("overflow","hidden");var c=a(this),n=a('<div id="bModal"></div>'),d=a(document),e=a(window),r=p(c,o.amsl),g=r[0],h=r[1],m=a.browser.msie&&parseInt(a.browser.version)==6&&typeof window.XMLHttpRequest!="object";this.close=function(){o=c.data("bPopup");i()};
return this.each(function(){if(!c.data("bPopup")){o.modal&&n.css(u()).appendTo(o.appendTo).animate({opacity:o.opacity},o.fadeSpeed);c.data("bPopup",o);s()}})};a.fn.bPopup.defaults={amsl:150,appendTo:"body",closeClass:"bClose",content:"ajax",contentContainer:null,escClose:true,fadeSpeed:250,follow:true,followSpeed:500,loadUrl:null,modal:true,modalClose:true,modalColor:"#000",opacity:0.7,scrollBar:true,vStart:null,zIndex:9999}})(jQuery);*/

/************************************************************************
 * @name: bPopup
 * @author: (c) Bjoern Klinggaard (http://dinbror.dk/bpopup)
 * @version: 0.5.1.min
 ************************************************************************/
(function(a){a.fn.bPopup=function(h,j){function s(){b.css({left:!o.follow[1]&&k?d:c.scrollLeft()+d,position:"absolute",top:!o.follow[0]&&l?e:c.scrollTop()+e,"z-index":o.zIndex}).appendTo(o.appendTo).hide(1,function(){a.isFunction(o.onOpen)&&o.onOpen.call(b);if(o.loadUrl!=null)switch(o.contentContainer=o.contentContainer==null?b:a(o.contentContainer),o.content){case "iframe":a('<iframe scrolling="no" frameborder="0"></iframe>').attr("src",o.loadUrl).appendTo(o.contentContainer);break;default:o.contentContainer.load(o.loadUrl)}}).fadeIn(o.fadeSpeed,function(){a.isFunction(j)&&j()});t()}function i(){o.modal&&a("#bModal").fadeOut(o.fadeSpeed,function(){a("#bModal").remove()});b.fadeOut(o.fadeSpeed,function(){o.loadUrl!=null&&o.contentContainer.empty()});o.scrollBar||a("html").css("overflow","auto");a("."+o.closeClass).die("click");a("#bModal").die("click");c.unbind("keydown.bPopup");f.unbind(".bPopup");b.data("bPopup",null);a.isFunction(o.onClose)&&setTimeout(function(){o.onClose.call(b)},o.fadeSpeed);return!1}function u(){if(n||v){var a=[c.height(),c.width()];return{"background-color":o.modalColor,height:a[0],left:m(),opacity:0,position:"absolute",top:0,width:a[1],"z-index":o.zIndex-1}}else return{"background-color":o.modalColor,height:"100%",left:0,opacity:0,position:"fixed",top:0,width:"100%","z-index":o.zIndex-1}}function t(){a("."+o.closeClass).live("click",i);o.modalClose&&a("#bModal").live("click",i).css("cursor","pointer");(o.follow[0]||o.follow[1])&&f.bind("scroll.bPopup",function(){b.stop().animate({left:o.follow[1]?c.scrollLeft()+d:d,top:o.follow[0]?c.scrollTop()+e:e},o.followSpeed)}).bind("resize.bPopup",function(){if(o.modal&&n){var a=[c.height(),c.width()];p.css({height:a[0],width:a[1],left:m()})}g=q(b,o.amsl);o.follow[0]&&(e=l?e:c.scrollTop()+g[0]);o.follow[1]&&(d=k?d:c.scrollLeft()+g[1]);b.stop().animate({left:d,top:e},o.followSpeed)});o.escClose&&c.bind("keydown.bPopup",function(a){a.which==27&&i()})}function m(){return f.width()<a("body").width()?0:(a("body").width()-f.width())/2}function q(a,b){var c=(f.height()-a.outerHeight(!0))/2-b,d=(f.width()-a.outerWidth(!0))/2+m();return[c<20?20:c,d]}a.isFunction(h)&&(j=h,h=null);o=a.extend({},a.fn.bPopup.defaults,h);o.scrollBar||a("html").css("overflow","hidden");var b=a(this),p=a('<div id="bModal"></div>'),c=a(document),f=a(window),g=q(b,o.amsl),l=o.position[0]!="auto",k=o.position[1]!="auto",e=l?o.position[0]:g[0],d=k?o.position[1]:g[1],r=navigator.userAgent.toLowerCase(),v=r.indexOf("iphone")!=-1,n=/msie 6/i.test(r)&&typeof window.XMLHttpRequest!="object";this.close=function(){o=b.data("bPopup");i()};return this.each(function(){b.data("bPopup")||(o.modal&&p.css(u()).appendTo(o.appendTo).animate({opacity:o.opacity},o.fadeSpeed),b.data("bPopup",o),s())})};a.fn.bPopup.defaults={amsl:50,appendTo:"body",closeClass:"bClose",content:"ajax",contentContainer:null,escClose:!0,fadeSpeed:250,follow:[!0,!0],followSpeed:500,loadUrl:null,modal:!0,modalClose:!0,modalColor:"#000",onClose:null,onOpen:null,opacity:0.7,position:["auto","auto"],scrollBar:!0,zIndex:9999}})(jQuery);

// Notifications Pop-Up functionality

	$(document).ready(function(){
	   		$("a.notifypop").bind('click', function(){
				var url = $(this).attr('href');
				$.ajax({
					type: "GET",
					dataType:"json",
					url: url,
					success: function(json){
						var len = json.length;
						var p = '';
						
						p += '<h4>商品编号：'+json[0].GoodsNO+'</h4>';
						p += '	<table width="100%">';
						p += '		<thead>';
						p += '			<tr>';
						p += '				<th>规格</th>';
						p += '				<th>库存</th>';
						p += '				<th>警戒</th>';
						p += '			</tr>';
						p += '		</thead>';
						p += '		<tbody>';
						
						for(var i=0; i<len; i++)
						{
							p += '		<tr>';
							p += '			<td>'+json[i].SpecName+'</td>';
							p += '			<td>'+json[i].RealStock+'</td>';
							p += '			<td>'+json[i].RealWarStock+'</td>';
							p += '		</tr>';
						}
						p += '		</tbody>';
						p += '	</table>';
						
						$('#notificationsbox').html(p);
					}
				});
				$("#notificationsbox").bPopup({follow:[false,false]});
		  	 	return false;
			});
		});
		
// 修改库存
$(document).ready(function(){
	$("a.editstock").bind('click', function(){
		var url = $(this).attr('href');
		var cururl = window.location.href;
		$.ajax({
			type: "GET",
			dataType:"json",
			url: url,
			success: function(json){
				var len = json.length;
				var p = '';
				var bpop = '';
				
				p += '<form action="index.php/goods/modify/'+json[0].GoodsID+'" method="post">';
				p += '<h4>商品编号：'+json[0].GoodsNO+'</h4>';
				p += '<table width="100%">';
				p += '	<thead>';
				p += '		<tr>';
				p += '			<th> 规 格 </th>';
				p += '			<th> 库 存 </th>';
				p += '			<th> 操 作 </th>';
				p += '			<th> 数 量 </th>';
				p += '			<th> 警 戒 </th>';
				/*p += '			<th>&nbsp;&nbsp;&nbsp;&nbsp;</th>';*/
				p += '		</tr>';
				p += '	</thead>';
				p += '	<tbody>';
				
				for(var i=0; i<len; i++)
				{
					p += '	<tr>';
					p += '		<td>'+json[i].SpecName+'</td>';
					/*p += '		<td><input type="text" id="smallbox" readonly="readonly" class="inputbox smallbox" name="relstock['+json[i].SpecID+']" value="'+json[i].RealStock+'" /></td>';*/
					p += '		<td><input type="text" id="smallbox" readonly="readonly" class="inputbox smallbox" name="'+json[i].SpecID+'[relstock]" value="'+json[i].RealStock+'" /></td>';
					p += '		<td>';
					/*p += '			<select name="opselect['+json[i].SpecID+']">';*/
					p += '			<select name="'+json[i].SpecID+'[opselect]">';
					p += '				<option value="add"> 入 库 </option>';
					p += '				<option value="sub"> 出 库 </option>';
					p += '			</select>';
					p += '		</td>';
					/*p += '		<td><input type="text" id="smallbox" class="inputbox smallbox" name="stock['+json[i].SpecID+']" /></td>';*/
					p += '		<td><input type="text" id="smallbox" class="inputbox smallbox" name="'+json[i].SpecID+'[stock]" /></td>';
					/*p += '		<td><input type="text" id="smallbox" class="inputbox smallbox" name ="stockWar['+json[i].SpecID+']" value="'+json[i].RealWarStock+'" /></td>';*/
					p += '		<td><input type="text" id="smallbox" class="inputbox smallbox" name ="'+json[i].SpecID+'[stockWar]" value="'+json[i].RealWarStock+'" /></td>';
					p += '	</tr>';
				}
				p += '		<tr>';
				p += '			<td colspan="5"><input id="modStock" type="submit" value=" 确 定 " class="btnalt width_sub" /></td>';
				p += '		</tr>';
				p += '		<input type="hidden" name="cururl" value="'+cururl+'" />';
				p += '	</tbody>';
				p += '</table>';
				p += '</form>';
				
				$('#notificationsbox').html(p);
				
				$("#modStock").click(function(){
					var re = /^[1-9]+[0-9]*]*$/;
					for(var j=0; j<len; j++)
					{
						svalname = json[j].SpecID + '[stock]';
						if(!re.test($("input[name='"+svalname+"']").val()) && $("input[name='"+svalname+"']").val() != "" && $("input[name='"+svalname+"']").val() != 0)
						{
							alert("操作库存输入有误，请重新输入！");
							return false;
						}
						
						valname = json[j].SpecID + '[relstock]';
						opvalue = json[j].SpecID + '[opselect]';
						if($("select[name='"+opvalue+"']").val() == 'sub' && $("input[name='"+valname+"']").val() - $("input[name='"+svalname+"']").val() < 0)
						{
							alert("不能负库存出库！请检查！");
							return false;
						}
						
						wvalname = json[j].SpecID + '[stockWar]';
						if(!re.test($("input[name='"+wvalname+"']").val()) && $("input[name='"+wvalname+"']").val() != "" && $("input[name='"+wvalname+"']").val() != 0)
						{
							alert("警戒库存输入有误，请重新输入！");
							return false;
						}
					}
				});
			}
		});
		$("#notificationsbox").bPopup({follow:[false,false]});
		return false;
	});
});


$(document).ready(function(){
 	$("#expexcel").click(function(){
		window.open("index.php/allocate/exporttoexcel");
	});
});

$(document).ready(function(){
 	$("#update").click(function(){
		var k=window.confirm("确定更新数据库？请确保已经成功导出配货单！此操作将不可逆。");
		if(k)
		{
			//window.open("index.php/allocate/updateDataBase");
			//$(this).attr('disabled',"true");
			url = "index.php/allocate/updateDataBase";
			$.ajax({
				type: "GET",
				dataType:"json",
				url: url,
				success: function(json){
					alert("更新成功！");
					$("#update").attr('disabled',"true");
				}
			});
		}
	});
});
	
// Charting script

$(document).ready(function(){
 	$('table.pie').visualize({type: 'pie', height: '300px', width: '620px'});
	$('table.bar').visualize({type: 'bar', height: '300px', width: '620px'});
	$('table.area').visualize({type: 'area', height: '300px', width: '620px'});
	$('table.line').visualize({type: 'line', height: '300px', width: '620px'});
});
		
// Tab Switching

	$(document).ready(function(){
		$("#tabs, #graphs").tabs();
	});

// Select all checkboxes

	$(document).ready(function(){
      $("#checkboxall").click(function()
      {
      var checked_status = this.checked;
      $("input[name=checkall]").each(function() {
      this.checked = checked_status;
      });
      });
      });

// Rich text editor/WYSIWYG

	$(document).ready(function() {
			$('#wysiwyg').wysiwyg();
		});